import { Channel, Token, Suggest } from '@/types/data'

const TOKEN_KEY = 'geek-h5-token'
const CHANNEL_GK = 'geek-channels'
const HISTORY_KEY = 'geek-history'

export function setToken(token: Token): void {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token))
}

export function getToken(): Token {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}')
}

export function removeToken(): void {
  localStorage.removeItem(TOKEN_KEY)
}

export function hasToken(): Boolean {
  return !!getToken().token
}

export function setChannel(channel: Channel[]): void {
  localStorage.setItem(CHANNEL_GK, JSON.stringify(channel))
}

export function getChannel(): Channel[] {
  return JSON.parse(localStorage.getItem(CHANNEL_GK) || '{}')
}

export function setHistory(keyword: Suggest) {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(keyword))
}

export function getHistory(): Suggest {
  return JSON.parse(localStorage.getItem(HISTORY_KEY) || '[]')
}
